#pragma once

/**
Usefull makro that does not generate the overhead of pgString::CharPointerToLPCWSTR()
@param szCharPointer String to be converted.
@param lpWcharArray Will be filled with szCharPointer, (use fixed size to circumvade heap allocations
*/
#define PG_STRING_CHARP_TO_LPCWSTR(szCharPointer, lpWcharArray) { \
										size_t makro_origsize = strlen(szCharPointer) + 1; \
										size_t makro_convertedChars = 0; \
										mbstowcs_s(&makro_convertedChars, lpWcharArray, makro_origsize, szCharPointer, _TRUNCATE); \
								}

/**
String utilities class.
Provides various conversion methods.
*/
class pgString
{
public:

	/**
	Converts a wchar_t to a std::string object.
	A char array would be deleted as soon as the function returns, since it's a pointer.
	That's why std::string is used here (returned by value).
	@param lpString String to be converted
	@returns std::string object containing the lpString
	*/
	static std::string LPCWSTRToStdString(LPCWSTR lpString)
	{
		size_t origsize = wcslen(lpString) + 1;
		const size_t newsize = 100;
		size_t convertedChars = 0;
		char nstring[newsize];
		wcstombs_s(&convertedChars, nstring, origsize, lpString, _TRUNCATE);
		std::string result(nstring);
		return result;
	}

	/**
	Convert a character pointer to a LPCWSTR, CAUTION: you need to delete the returned string yourself atm.
	Consider using PG_CHARP_TO_LPCWSTR as using this function will allocate heap memory (slow, managament overhead).
	@param szString String to be converted
	@returns newly created LPCWSTR
	*/
	static LPCWSTR CharPointerToLPCWSTR(const char* szString)
	{ 
		size_t origsize = strlen(szString) + 1;
		const size_t newsize = 100;
		size_t convertedChars = 0;
		wchar_t wcstring[newsize];
		mbstowcs_s(&convertedChars, wcstring, origsize, szString, _TRUNCATE);

		wchar_t *finalString = new wchar_t[convertedChars];

		for (int i = 0; i < convertedChars; ++i)
		{
			finalString[i] = wcstring[i];
		}
		return finalString;
	}
};